package com.np.ffmpegjni4;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.fonts.Font;
import android.media.Image;

import java.io.ByteArrayOutputStream;

public class ImageUtil {

    /**
     * 由字符串生成 Bitmap 图片
     *
     * @param text
     * @return
     */
    public static Bitmap stringToBitmap(String text) {

        int textSize = 10;
        int width = 100;
        int height = 100;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        // 设置背景颜色
        canvas.drawColor(Color.WHITE);

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(textSize);

        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);

        // 计算文本的宽度和高度
        float x = (bitmap.getWidth() - bounds.width()) / 2f;
        float y = (bitmap.getHeight() - bounds.height()) / 2f;

        // 在画布上绘制文本
        canvas.drawText(text, x, y, paint);

        return bitmap;
    }
}
